package gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.beans.PropertyVetoException;
import java.io.File;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.prefs.Preferences;

import javax.swing.AbstractButton;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JComboBox;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.table.DefaultTableModel;

import org.apache.log4j.Logger;

import utils.ChatParser;
import connection.Reader;
import database.MySQLConnection;

public class Desktop extends JFrame implements ActionListener, WindowListener {

	private static final long serialVersionUID = 1L;

	private JDesktopPane desktop;

	private JButton save;
	private JButton canc;

	private JTextField email;
	private JTextField bestRating;
	private JTextArea note;
	private JComboBox users;
	private JComboBox users2;
	private JLabel mail;
	private JLabel won;
	private JLabel lost;
	private JLabel lnote;

	private String language;

	private ResourceBundle messages;

	private Preferences prefs;

	private JPanel back;

	private JTable inbox;

	private Object[][] data;

	private DefaultTableModel model;

	private JCheckBox checkDatabase;

	private JButton okDatabase;

	private JInternalFrame fDatabase;

	private static JMenuItem itemEdit;

	private static JMenuItem itemShow;

	private static JMenuItem itemShowTour;

	private static JMenuItem gamesShow;

	private JMenuBar menuBar;

	private JMenu menu;

	private BoardWin board;

	private LanguageWin lanWin;

	private static InfoWin inf;

	protected final static Logger logger = Logger.getLogger("Log");

	private static Desktop frame;

	private String iconPath = System.getProperty("user.dir") + File.separator
			+ "Resources" + File.separator + "icon" + File.separator;

	private ConsoleWin console;

	private SoughtWin sought;

	private ChatWin chatWin;

	private JCheckBoxMenuItem winConsole;

	private JCheckBoxMenuItem winSought;

	private JCheckBoxMenuItem winBoard;

	private JCheckBoxMenuItem winChat;

	private JLabel draw;

	private JButton finish;

	private JInternalFrame player_info_frame;

	private JInternalFrame edit_player_frame;

	private LoginWin logWin;

	private JButton cancDatabase;

	private JTextField databaseLogin;

	private JPasswordField databasePassword;

	private ChalengeWin chalange;

	private SeekWin seek;

	private ChangePassWin changePass;

	private JMenuItem menuChangePassword;

	private JMenuItem gameSeek;

	private JMenuItem gameChalenge;

	private static JMenuItem database;

	private static ChalengeInfoWin chalenge;

	/**
	 * Vytvari GUI
	 */
	public Desktop() {
		super("DoveChess");

		ImageIcon image = new ImageIcon(iconPath + "package_games_strategy.png");
		this.setIconImage(image.getImage());

		prefs = Preferences.userRoot();
		language = prefs.get("Language", "cs");
		String languageSetting = prefs.get("LanguageSetting", language);
		;
		prefs.put("Language", languageSetting);

		Locale currentLocale = new Locale(languageSetting);
		messages = ResourceBundle.getBundle("Lang", currentLocale);

		desktop = new JDesktopPane();
		setSize(1024, 730);

		setContentPane(desktop);

		setJMenuBar(createMenuBar());

		logWin = new LoginWin(messages.getString("Login"));
		logWin.setLocation(200, 200);
		logWin.setWindow(desktop);

		lanWin = new LanguageWin(messages.getString("LangSet"));
		lanWin.setLocation(200, 200);
		desktop.add(lanWin);
		lanWin.setVisible(false);

		console = new ConsoleWin(messages.getString("Console"));
		console.setLocation(592, 411);
		console.setVisible(false);
		desktop.add(console.getWindow());

		board = new BoardWin(messages.getString("Board"));
		board.setWindow();
		board.setLocation(2, 2);
		board.setVisible(false);
		desktop.add(board);

		inf = new InfoWin(messages.getString("Info"));
		desktop.add(inf);
		inf.setVisible(false);

		chalenge = new ChalengeInfoWin(messages.getString("Chalenge"));
		desktop.add(chalenge);
		chalenge.setVisible(false);

		chatWin = new ChatWin(messages.getString("Chat"));
		chatWin.setLocation(2, 441);
		chatWin.setVisible(false);
		desktop.add(chatWin);

		sought = new SoughtWin(messages.getString("Sought"));
		sought.setLocation(592, 2);
		sought.setVisible(false);
		desktop.add(sought.getWindow());

		chalange = new ChalengeWin(messages.getString("Chalenge"));
		chalange.setVisible(false);
		desktop.add(chalange.getWindow());

		seek = new SeekWin(messages.getString("Seek"));
		seek.setVisible(false);
		desktop.add(seek.getWindow());

		changePass = new ChangePassWin(messages.getString("Change_password"));
		changePass.setVisible(false);
		desktop.add(changePass.getWindow());

		MySQLConnection.connect();

		LoginListener actionListener = new LoginListener() {
			public void connecting(LoginEvent e) {
				sought.setVisible(true);
				winSought.setSelected(true);
				board.setVisible(true);
				winBoard.setSelected(true);
				chatWin.setVisible(true);
				winChat.setSelected(true);
				console.setVisible(true);
				winConsole.setSelected(true);
				console.setLogin(e.getLogin());
				MySQLConnection.setMyLogin(e.getLogin());
				console.setPassword(e.getPassword());
				console.connect();
				sought.connect();
				board.connect();
				new ChatParser(chatWin);
				menuChangePassword.setEnabled(true);
				gameChalenge.setEnabled(true);
				gameSeek.setEnabled(true);
			}
		};

		logWin.addLoginListener(actionListener);
		desktop.add(logWin.getWindow());
		desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
		addWindowListener(this);
	}

	protected JMenuBar createMenuBar() {
		menuBar = new JMenuBar();

		menu = new JMenu(messages.getString("File"));
		menu.setMnemonic(KeyEvent.VK_D);
		menuBar.add(menu);

		menuChangePassword = new JMenuItem(messages
				.getString("Change_password"));
		menuChangePassword.setActionCommand("changePassword");
		menuChangePassword.addActionListener(this);
		menuChangePassword.setEnabled(false);
		menu.add(menuChangePassword);

		menu.addSeparator();

		JMenuItem menuItem = new JMenuItem(messages.getString("Quit"));
		menuItem.setMnemonic(KeyEvent.VK_Q);
		menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,
				ActionEvent.ALT_MASK));
		menuItem.setActionCommand("quit");
		menuItem.addActionListener(this);
		menu.add(menuItem);

		JMenu menuDatabase = new JMenu(messages.getString("Database"));
		menu.setMnemonic(KeyEvent.VK_D);
		menuBar.add(menuDatabase);

		itemEdit = new JMenuItem(messages.getString("EditPlayer"));
		itemEdit.setActionCommand("edit");
		itemEdit.addActionListener(this);
		menuDatabase.add(itemEdit);

		itemShow = new JMenuItem(messages.getString("ShowPlayer"));
		itemShow.setActionCommand("show");
		itemShow.addActionListener(this);
		menuDatabase.add(itemShow);

		itemShowTour = new JMenuItem(messages.getString("ShowTour"));
		itemShowTour.setActionCommand("showtour");
		itemShowTour.addActionListener(this);
		menuDatabase.add(itemShowTour);

		gamesShow = new JMenuItem(messages.getString("GamesShow"));
		gamesShow.setActionCommand("gameshow");
		gamesShow.addActionListener(this);
		menuDatabase.add(gamesShow);

		JMenu menuSettings = new JMenu(messages.getString("Settings"));
		menuBar.add(menuSettings);

		JMenuItem language = new JMenuItem(messages.getString("Language"));
		language.setActionCommand("language");
		language.addActionListener(this);
		menuSettings.add(language);

		database = new JMenuItem(messages.getString("Database"));
		database.setActionCommand("database");
		database.addActionListener(this);
		menuSettings.add(database);

		JMenu menuGame = new JMenu(messages.getString("Game"));
		menuBar.add(menuGame);

		gameSeek = new JMenuItem(messages.getString("Seek"));
		gameSeek.setActionCommand("winseek");
		gameSeek.addActionListener(this);
		gameSeek.setEnabled(false);
		menuGame.add(gameSeek);

		gameChalenge = new JMenuItem(messages.getString("Chalenge"));
		gameChalenge.setActionCommand("winchalenge");
		gameChalenge.addActionListener(this);
		gameChalenge.setEnabled(false);
		menuGame.add(gameChalenge);

		JMenu menuWindow = new JMenu(messages.getString("Window"));
		menuBar.add(menuWindow);

		winConsole = new JCheckBoxMenuItem(messages.getString("Console"));
		winConsole.setActionCommand("winconsole");
		winConsole.addActionListener(this);
		menuWindow.add(winConsole);

		winSought = new JCheckBoxMenuItem(messages.getString("Sought"));
		winSought.setActionCommand("winsought");
		winSought.addActionListener(this);
		menuWindow.add(winSought);

		winBoard = new JCheckBoxMenuItem(messages.getString("Board"));
		winBoard.setActionCommand("winboard");
		winBoard.addActionListener(this);
		menuWindow.add(winBoard);

		winChat = new JCheckBoxMenuItem(messages.getString("Chat"));
		winChat.setActionCommand("winchat");
		winChat.addActionListener(this);
		menuWindow.add(winChat);

		JMenu menuHelp = new JMenu(messages.getString("Help"));
		menuBar.add(menuHelp);

		JMenuItem helpAbout = new JMenuItem(messages.getString("About"));
		helpAbout.setActionCommand("helpabout");
		helpAbout.addActionListener(this);
		menuHelp.add(helpAbout);

		itemEdit.setEnabled(Boolean
				.valueOf(prefs.get("checkDatabase", "false")));
		itemShow.setEnabled(Boolean
				.valueOf(prefs.get("checkDatabase", "false")));
		itemShowTour.setEnabled(Boolean.valueOf(prefs.get("checkDatabase",
				"false")));
		gamesShow.setEnabled(Boolean.valueOf(prefs
				.get("checkDatabase", "false")));

		return menuBar;
	}

	public void actionPerformed(ActionEvent e) {

		Object action = e.getSource();
		if ("disconnect".equals(e.getActionCommand())) {
			logger.debug("exit");
			Reader.sendString("exit");
			Reader.disConnect();
		}
		if ("connect".equals(e.getActionCommand())) {
			logWin.setVisible(true);
			try {
				logWin.setSelected(true);
			} catch (PropertyVetoException e1) {
				logger.error("", e1);
			}
		}
		if ("quit".equals(e.getActionCommand())) {
			quit();
		}
		if ("edit".equals(e.getActionCommand())) {
			editPlayer();
		}
		if ("showtour".equals(e.getActionCommand())) {
			showTournament();
		}
		if ("show".equals(e.getActionCommand())) {
			showPlayer();
		}
		if ("gameshow".equals(e.getActionCommand())) {
			showGames();
		}
		if ("database".equals(e.getActionCommand())) {
			databaseFrame();
		}
		if ("language".equals(e.getActionCommand())) {
			lanWin.setVisible(true);
		}
		if ("winsought".equals(e.getActionCommand())) {
			AbstractButton aButton = (AbstractButton) e.getSource();
			boolean selected = aButton.getModel().isSelected();
			if (selected) {
				sought.setVisible(true);
			} else {
				sought.setVisible(false);
			}
		}
		if ("winboard".equals(e.getActionCommand())) {
			AbstractButton aButton = (AbstractButton) e.getSource();
			boolean selected = aButton.getModel().isSelected();
			if (selected) {
				board.setVisible(true);
			} else {
				board.setVisible(false);
			}
		}
		if ("winconsole".equals(e.getActionCommand())) {
			AbstractButton aButton = (AbstractButton) e.getSource();
			boolean selected = aButton.getModel().isSelected();
			if (selected) {
				console.setVisible(true);
			} else {
				console.setVisible(false);
			}
		}
		if ("winchat".equals(e.getActionCommand())) {
			AbstractButton aButton = (AbstractButton) e.getSource();
			boolean selected = aButton.getModel().isSelected();
			if (selected) {
				chatWin.setVisible(true);
			} else {
				chatWin.setVisible(false);
			}
		}
		if ("winseek".equals(e.getActionCommand())) {
			seek.setVisible(true);
		}
		if ("winchalenge".equals(e.getActionCommand())) {
			chalange.setVisible(true);
		}
		if ("changePassword".equals(e.getActionCommand())) {
			changePass.setVisible(true);
		}
		if ("helpabout".equals(e.getActionCommand())) {
			infoMessage("DoveChess 0.12");
		}
		if (action.equals(okDatabase)) {
			if (checkDatabase.isSelected()) {
				prefs.put("checkDatabase", "true");
			} else {
				prefs.put("checkDatabase", "false");
			}
			itemEdit.setEnabled(Boolean.valueOf(prefs.get("checkDatabase",
					"false")));
			itemShow.setEnabled(Boolean.valueOf(prefs.get("checkDatabase",
					"false")));
			itemShowTour.setEnabled(Boolean.valueOf(prefs.get("checkDatabase",
					"false")));
			gamesShow.setEnabled(Boolean.valueOf(prefs.get("checkDatabase",
					"false")));
			prefs.put("databaseLogin", databaseLogin.getText());
			prefs.put("databasePassword", String.valueOf(databasePassword
					.getPassword()));
			MySQLConnection.connect();
			fDatabase.setVisible(false);
		}
		if (action.equals(save)) {
			int best = 0;
			if (!bestRating.getText().equals("")) {
				best = Integer.valueOf(bestRating.getText());
			}
			MySQLConnection.editPlayer(users.getSelectedItem().toString(),
					email.getText(), best, note.getText());
		}
		if (action.equals(finish)) {
			player_info_frame.setVisible(false);
		}
		if (action.equals(canc)) {
			edit_player_frame.setVisible(false);
		}
		if (action.equals(cancDatabase)) {
			fDatabase.setVisible(false);
		}

		if (action.equals(users)) {
			logger.debug("akce");
			String[] res = new String[5];
			res = MySQLConnection.getPlayersInfo(users.getSelectedItem()
					.toString());
			email.setText(res[0]);
			bestRating.setText(res[1]);
			note.setText(res[2]);
		}
		if (action.equals(users2)) {
			String[] res = new String[5];
			res = MySQLConnection.getPlayersInfo(users2.getSelectedItem()
					.toString());
			mail.setText(res[0]);
			won.setText(res[3]);
			lost.setText(res[5]);
			lnote.setText(res[2]);
			draw.setText(res[4]);
		}
	}

	protected void databaseFrame() {
		fDatabase = new JInternalFrame(messages.getString("Database"), true,
				true, false, false);
		fDatabase.setVisible(true);

		JPanel pan = new JPanel();
		pan.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();

		c.insets = new Insets(10, 10, 10, 10);

		c.gridx = 0;
		c.gridy = 2;
		JLabel ldatabase = new JLabel(messages.getString("Use_database"));
		pan.add(ldatabase, c);
		c.gridx = 1;
		checkDatabase = new JCheckBox();
		if (Boolean.valueOf(prefs.get("checkDatabase", "false"))) {
			checkDatabase.setSelected(true);
		}
		pan.add(checkDatabase, c);

		c.gridx = 0;
		c.gridy = 3;
		JLabel databaseLoginLabel = new JLabel(messages.getString("Login")
				+ ":");
		pan.add(databaseLoginLabel, c);
		c.gridx = 1;
		databaseLogin = new JTextField();
		databaseLogin.setPreferredSize(new Dimension(100, 20));
		databaseLogin.setText(prefs.get("databaseLogin", "root"));
		pan.add(databaseLogin, c);
		c.gridx = 0;
		c.gridy = 4;
		JLabel databasePasswordLabel = new JLabel(messages
				.getString("Password"));
		pan.add(databasePasswordLabel, c);
		c.gridx = 1;
		databasePassword = new JPasswordField();
		databasePassword.setPreferredSize(new Dimension(100, 20));
		databasePassword.setText(prefs.get("databasePassword", ""));
		pan.add(databasePassword, c);
		c.gridx = 0;
		c.gridy = 5;
		okDatabase = new JButton(messages.getString("Ok"));
		okDatabase.addActionListener(this);
		pan.add(okDatabase, c);

		c.gridx = 1;
		cancDatabase = new JButton(messages.getString("Cancel"));
		cancDatabase.addActionListener(this);
		pan.add(cancDatabase, c);

		fDatabase.add(pan);

		fDatabase.setLocation(30, 30);
		fDatabase.pack();
		fDatabase.setPreferredSize(new Dimension(400, 400));
		desktop.add(fDatabase);
		try {
			fDatabase.setSelected(true);
		} catch (java.beans.PropertyVetoException e) {
		}
	}

	private void showGames() {
		JInternalFrame frame = new JInternalFrame(messages
				.getString("Show_games_info"), true, true, false, false);
		frame.setVisible(true);
		back = new JPanel();

		data = MySQLConnection.getGamesInfo();

		String[] columnNames = { messages.getString("Oponent"),
				messages.getString("Style"), messages.getString("Date"),
				messages.getString("Time"), messages.getString("Result") };
		model = new DefaultTableModel(data, columnNames);
		inbox = new JTable(model);
		inbox.setPreferredScrollableViewportSize(new Dimension(600, 150));
		inbox.setBackground(Color.WHITE);

		JScrollPane top = new JScrollPane(inbox);
		back.add(top);

		getContentPane().add(back);

		frame.add(back);
		back.setPreferredSize(new Dimension(630, 180));
		frame.setLocation(30, 30);
		frame.pack();
		desktop.add(frame);
		try {
			frame.setSelected(true);
		} catch (java.beans.PropertyVetoException e) {
			logger.error("", e);
		}
	}

	private void showTournament() {
		JInternalFrame frame = new JInternalFrame(messages
				.getString("Show_tournaments_info"), true, true, false, false);
		frame.setVisible(true);
		back = new JPanel();

		data = MySQLConnection.getTournamentsInfo();

		String[] columnNamesTournament = { messages.getString("Style"),
				messages.getString("Time"), messages.getString("Players"),
				messages.getString("Result"),
				messages.getString("Starting_time"), messages.getString("Date") };
		model = new DefaultTableModel(data, columnNamesTournament);
		inbox = new JTable(model);
		inbox.setPreferredScrollableViewportSize(new Dimension(600, 150));
		inbox.setBackground(Color.WHITE);

		JScrollPane top = new JScrollPane(inbox);
		back.add(top);

		getContentPane().add(back);

		frame.add(back);
		back.setPreferredSize(new Dimension(630, 180));
		frame.setLocation(30, 30);
		frame.pack();
		desktop.add(frame);
		try {
			frame.setSelected(true);
		} catch (java.beans.PropertyVetoException e) {
			logger.error("", e);
		}
	}

	private void showPlayer() {
		player_info_frame = new JInternalFrame(messages
				.getString("Player_info"), true, true, false, false);
		player_info_frame.setVisible(true);

		JPanel pan = new JPanel();
		pan.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();

		c.insets = new Insets(10, 10, 10, 10);

		c.gridx = 0;
		c.gridy = 0;
		JLabel lPlayer = new JLabel(messages.getString("Player") + ":");
		pan.add(lPlayer, c);
		MySQLConnection.getPlayersNames();

		String[] pla = MySQLConnection.getPlayersNames();

		c.gridx = 1;
		users2 = new JComboBox(pla);
		users2.setSelectedIndex(0);
		users2.addActionListener(this);
		pan.add(users2);

		c.gridx = 0;
		c.gridy = 1;
		JLabel llogin = new JLabel(messages.getString("Email") + ":");
		pan.add(llogin, c);

		c.gridx = 1;
		mail = new JLabel();
		mail.setPreferredSize(new Dimension(100, 25));
		pan.add(mail, c);

		c.gridx = 0;
		c.gridy = 2;
		JLabel lpass = new JLabel(messages.getString("Wins") + ":");
		pan.add(lpass, c);

		c.gridx = 1;
		won = new JLabel();
		won.setPreferredSize(new Dimension(100, 25));
		pan.add(won, c);

		c.gridx = 0;
		c.gridy = 3;
		JLabel llost = new JLabel(messages.getString("Loses") + ":");
		pan.add(llost, c);

		c.gridx = 1;
		draw = new JLabel();
		draw.setPreferredSize(new Dimension(100, 25));
		pan.add(draw, c);

		c.gridx = 0;
		c.gridy = 4;
		JLabel ldraw = new JLabel(messages.getString("Draws") + ":");
		pan.add(ldraw, c);

		c.gridx = 1;
		lost = new JLabel();
		lost.setPreferredSize(new Dimension(100, 25));
		pan.add(lost, c);

		c.gridx = 0;
		c.gridy = 5;
		JLabel lrate = new JLabel(messages.getString("Note") + ":");
		pan.add(lrate, c);

		c.gridx = 1;
		lnote = new JLabel();
		lnote.setPreferredSize(new Dimension(100, 25));
		pan.add(lnote, c);

		c.gridx = 0;
		c.gridy = 6;
		c.gridwidth = 2;
		finish = new JButton(messages.getString("Finish"));
		finish.addActionListener(this);
		pan.add(finish, c);

		player_info_frame.add(pan);

		player_info_frame.setLocation(30, 30);
		player_info_frame.pack();
		player_info_frame.setPreferredSize(new Dimension(400, 400));
		desktop.add(player_info_frame);
		try {
			player_info_frame.setSelected(true);
		} catch (java.beans.PropertyVetoException e) {
			logger.error("", e);
		}

	}

	private void editPlayer() {
		edit_player_frame = new JInternalFrame(messages
				.getString("Edit_player"), true, true, false, false);
		edit_player_frame.setVisible(true);

		JPanel pan = new JPanel();
		pan.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();

		c.insets = new Insets(10, 10, 10, 10);

		c.gridx = 0;
		c.gridy = 0;
		JLabel lPlayer = new JLabel(messages.getString("Player") + ":");
		pan.add(lPlayer, c);
		MySQLConnection.getPlayersNames();

		String[] petStrings = MySQLConnection.getPlayersNames();

		c.gridx = 1;
		users = new JComboBox(petStrings);
		users.setSelectedIndex(0);
		users.addActionListener(this);
		pan.add(users);

		c.gridx = 0;
		c.gridy = 1;
		JLabel llogin = new JLabel(messages.getString("Email") + ":");
		pan.add(llogin, c);

		c.gridx = 1;
		email = new JTextField();
		email.setPreferredSize(new Dimension(100, 25));
		pan.add(email, c);

		c.gridx = 0;
		c.gridy = 2;
		JLabel lpass = new JLabel(messages.getString("Best_rating") + ":");
		pan.add(lpass, c);

		c.gridx = 1;
		bestRating = new JTextField();
		bestRating.setPreferredSize(new Dimension(100, 25));
		pan.add(bestRating, c);

		c.gridx = 0;
		c.gridy = 3;
		JLabel lrate = new JLabel(messages.getString("Note") + ":");
		pan.add(lrate, c);

		c.gridx = 1;
		note = new JTextArea();
		note.setPreferredSize(new Dimension(100, 75));
		pan.add(note, c);

		c.gridx = 0;
		c.gridy = 4;
		save = new JButton(messages.getString("Save"));
		save.addActionListener(this);
		pan.add(save, c);

		c.gridx = 1;
		canc = new JButton(messages.getString("Cancel"));
		canc.addActionListener(this);
		pan.add(canc, c);

		edit_player_frame.add(pan);

		edit_player_frame.setLocation(30, 30);
		edit_player_frame.pack();
		edit_player_frame.setPreferredSize(new Dimension(400, 400));
		desktop.add(edit_player_frame);
		try {
			edit_player_frame.setSelected(true);
		} catch (java.beans.PropertyVetoException e) {
			logger.error("", e);
		}
	}

	protected void quit() {
		logger.debug("exit");
		MySQLConnection.updateSpendTime();
		System.exit(0);
	}

	/**
	 * Zobrazuje informativni zpravu na obrazovce
	 * 
	 * @param text
	 *            text ktery chceme zobrazit
	 */
	public static void infoMessage(String text) {
		inf.setText(text);
		inf.setVisible(true);
	}

	/**
	 * Zobrazuje informaci o vyzve
	 * 
	 * @param text
	 *            parametry vyzvy
	 */
	public static void chalenge(String text) {
		chalenge.setText(text);
		chalenge.setVisible(true);
	}

	/**
	 * Znepristupnuje pristup k funkcim databaze
	 */
	public static void disableDatabase() {
		database.setEnabled(false);
		itemEdit.setEnabled(false);
		itemShow.setEnabled(false);
		itemShowTour.setEnabled(false);
		gamesShow.setEnabled(false);
	}

	public static void createAndShowGUI() {
		JFrame.setDefaultLookAndFeelDecorated(false);
		frame = new Desktop();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}

	public void windowActivated(WindowEvent arg0) {
	}

	public void windowClosed(WindowEvent arg0) {
	}

	public void windowClosing(WindowEvent arg0) {
		System.out.println("KONEC");
		MySQLConnection.updateSpendTime();
	}

	public void windowDeactivated(WindowEvent arg0) {
	}

	public void windowDeiconified(WindowEvent arg0) {
	}

	public void windowIconified(WindowEvent arg0) {
	}

	public void windowOpened(WindowEvent arg0) {
	}
}
